<template lang="pug">
	view
		view.search(@click="gotoSearch" style="margin:20rpx;")
			u-search(placeholder="搜索产品"  shape="round" :clearabled="true" :showAction="false"  v-model="search" )
		category(:categoryList="categoryList" :subCategoryList="commoditys"
				@categoryMainClick="categoryMainClick" @categorySubClick="categorySubClick")
</template>

<script>
	import category from "../../components/qiyue-category/qiyue-category.vue"
	export default {
		 components:{
		     category
		},
		data() {
			return {
				Rimgurl:'http://47.102.203.158:2001/commodity/',
				categoryList:[],
				commoditys:[]
			}
		},
		methods: {
			getTypeList(){
				uni.$u.http.get('/types').then(res=>{
				
					this.categoryList = res.data.data
					this.categoryList.unshift({
						typeid:-1,
						name:'全部'
					})
					
				})
			},
			getcommoditys(typeid){
				let Url = '/commoditys?'
				if(typeid !== -1){
					Url+='typeid='+ typeid
				}
				uni.$u.http.get(Url).then(res=>{
					this.commoditys = res.data.data
					this.listLength = this.commoditys.length
					
					for (let i = 0; i < this.listLength; i++) {
						let zz = this.commoditys[i].imgurl 
						 
						 this.$set(this.commoditys[i],"imgurl",this.Rimgurl+zz)
					}
					
					if(typeid == -1){
						uni.$u.randomArray(this.commoditys)
					}
					console.log(this.commoditys);
				})
			},
			categoryMainClick(category){
				// this.subCategoryList = category.subCategoryList;
				this.getcommoditys(category.typeid)
				
			},
			categorySubClick(category){
				uni.navigateTo({
					url:'/pages/commodity/commodity?cid='+category.cid
				})
			},
			gotoSearch(){
				uni.navigateTo({
					url:"../search/search"
				})
			},
		},
		onShow() {
			this.getTypeList()
			
		},
		onLoad() {
			this.getcommoditys(-1)
		}
	}
</script>

<style>

</style>
